package com.security.door.controller.cardManage;

import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.csCardissue;
import com.security.door.model.dto.csCardissueDto;
import com.security.door.model.vo.csCardissueVo;
import com.security.door.service.IcsCardissueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - csCardissueController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class csCardissueController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IcsCardissueService icsCardissueService;

    @GetMapping("/csCardissue/queryListWithPage/{persionCode}")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper<PageInfo<csCardissueVo>> queryListWithPage(@PathVariable("persionCode") String persionCode) {
        csCardissueDto entityDto = new csCardissueDto();
        if (!"null".equals(persionCode))
            entityDto.setPersioncode(persionCode);
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);
        var list = icsCardissueService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping("/csCardIssue/deleteMore/{guids}")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper deleteMore(@PathVariable("guids") String guids) {
        icsCardissueService.deleteByIds(guids);
        return WrapMapper.ok();
    }

    @PostMapping("/csCardIssue/Add")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper Add(@RequestBody csCardissue entity) {
        if (null != entity) {
            if (icsCardissueService.hasIssue(entity.getCardno()))
                return WrapMapper.error("卡片已发行");
            entity.setGuid(UUID.randomUUID().toString());
            entity.setIssuetime(new Date());
            entity.setCreatetime(new Date());
            icsCardissueService.save(entity);
        }
        return WrapMapper.ok("操作成功");
    }
}
